let Player = function (playerInfo, ws, GameCtr) {
	// 玩家信息
	this.playerInfo = playerInfo;
	// 连接对象
	this.ws = ws;
	this.roomID = undefined;
	this.seatID = undefined;
	this.color = undefined;
	this.GameCtr = GameCtr;
	let self = this;

	// 监听连接
	ws.onmessage = function (msg) {
		// 解析
		let pak = JSON.parse(msg.data);
		console.log(pak.type);
		pak["data"] = self[pak.type](pak.data);
		self.sendMsg(pak);
	};
};

// 给玩家发信息
Player.prototype.sendMsg = function (data) {
	this.ws.send(JSON.stringify(data));
};

//监听玩家进入房间
Player.prototype.enterRoom = function (data) {
	this.GameCtr.roomManager.roomAddPlayer(
		this,
		data,
		this.GameCtr.Protocol.REFRESHROOM
	);
	//广播给其他玩家
	let bag = {
		accountID: this.playerInfo.accountID,
		roomID: this.roomID,
		seatID: this.seatID,
	};

	let pak = {
		type: this.GameCtr.Protocol.ENTERROOM,
		data: { err: null, info: bag },
		isGuangbo: true,
	};
	this.GameCtr.playerManager.broadcastToAllPlayer(pak);
	return { err: null, info: true };
};

Player.prototype.enterHall = function (pak) {
	return this.GameCtr.roomManager.getAllRoomInfo(pak);
};
Player.prototype.startGame = function (pak) {
	return this.GameCtr.roomManager.startGame(pak);
};
Player.prototype.roomMsg = function (pak) {
	return this.GameCtr.roomManager.roomMsg(pak);
};

Player.prototype.moveChess = function (pak) {
	return this.GameCtr.roomManager.roomMsg(pak);
};

module.exports = Player;
